'''
抓取表情党图片
'''
import requests
from lxml import etree
import os

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'
}
url = 'https://qq.yh31.com/xq/wq/'
res = requests.get(url, headers=headers)
html = res.content.decode()
# print(html)
tree = etree.HTML(html)
# 获取图片的src地址
# //*[@id="main_bblm"]/div[1]/div[3]/dl/li[1]/dt/a/img
# //*[@id="main_bblm"]/div[1]/div[3]/dl/li[2]/dt/a/img
src_list = tree.xpath('//div[@class="sr"]//img/@data-src')
print(src_list)
#  创建目录
dir = './img'
if not os.path.exists(dir):
    os.mkdir(dir)
# 循环起名称
i = 0
for src in src_list:
    # 下载图片
    with open(os.path.join(dir, str(i) + '.jpg'), 'wb') as f:
        f.write(requests.get(src, headers=headers).content)
    print('图片下载完成======', i)
    i += 1
